package live.jipiao.dao;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import live.api.impl.FlightReqRes;
import live.common.AirParam;



public class WebServicesSub {
	
	static WebServicesSub instance=null;
	
	public synchronized static WebServicesSub getInstance() {
		if(instance==null){
			instance = new WebServicesSub();
		}
		return instance;
	}
	
	/**
	 * 封装传递参数，并返回接口处理结果
	 * @param parameters
	 * @return
	 * @throws UnsupportedEncodingException
	 * @throws IOException
	 */
	public String returnResult(Map<String, String> parameters) throws UnsupportedEncodingException, IOException {
		String postData = "";
		if (parameters != null && parameters.size() > 0) {
			List keys = new ArrayList(parameters.keySet());
			Collections.sort(keys);
			for (int i = 0; i < keys.size(); i++) {
				String key = (String) keys.get(i);
				Object value = parameters.get(key);
				if (i == keys.size() - 1) {
					postData += key + "=" + value;
				} else {
					postData += key + "=" + value + "&";
				}
			}
		}
		return returnResult(postData,parameters);
	}
	
	/**
	 * 提交信息到接口
	 * @param strData
	 * @return
	 * @throws UnsupportedEncodingException
	 * @throws IOException
	 */
	private String returnResult(String strData,Map map) throws UnsupportedEncodingException, IOException{
		String encodeing="GB2312";
		map.put("service_id", map.get("service"));
		map.put("requestData", strData);
		String spec=AirParam.POST_URL.toString();
		URL url = new URL(spec);// 提交地址
		HttpURLConnection httpURLConnection = (HttpURLConnection) url
				.openConnection();
		httpURLConnection.setDoOutput(true);// 打开写入属性
		httpURLConnection.setDoInput(true);// 打开读取属性
		httpURLConnection.setRequestMethod("POST");// 设置提交方法
		httpURLConnection.addRequestProperty("ContentType",
				"application/x-www-form-urlencoded");
		httpURLConnection.setConnectTimeout(1000 * 60 * 5);// 连接超时时间
		httpURLConnection.setReadTimeout(1000 * 60 * 5);
		httpURLConnection.connect();
		BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
				httpURLConnection.getOutputStream(), "GB2312"));
		out.write(strData);// 要post的数据，多个以&符号分割
		out.flush();
		out.close();
		BufferedReader in = new BufferedReader(new InputStreamReader(
				(InputStream) httpURLConnection.getInputStream(),encodeing));
		String line = null;
		StringBuilder sb = new StringBuilder();
		while ((line = in.readLine()) != null) {
			sb.append(line);
		}
		if(map.get("service").equals("GetFlight")){
			if(sb.toString().contains("<is_success>F</is_success>")){
			map.put("resData", sb.toString());
			}
		}
		FlightReqRes.insertSendRequest(map);
		in.close();
		httpURLConnection.disconnect();// 断开连接
		return sb.toString();
	}
}
